<?php namespace Admin\Controller;

/**
 *货品列表控制器
 */
class GoodsListController extends CommonController
{
	protected $db;
	/**
	 * 构造方法
	 */
	public function __init()
	{
		$this->db = new \Common\Model\GoodsList;
	}
	/**
	 * 首页
	 */
	public function index()
	{
		//获得gid和type_tid
		$gid = Q('get.gid',0,'intval');				
		$type_tid = Q('get.type_tid',0,'intval');
		//获得类型属性表数据
		$oldData = Db::table('typeattr')
					       ->where('type_tid','=',$type_tid)
						   ->where('class','=',2)
						   ->get();
		//p($oldData);
		View::with('oldData',$oldData);
		//获得商品属性表数据
		//循环压数据
		foreach ($oldData as $k => $v) {
			$oldData[$k]['tavalue'] = Db::table('goods_attr')
							 ->where('typeattr_taid','=',$v['taid'])
			                 ->where('goods_gid','=',$gid)
			                 ->get();
		}
		p($oldData);
		/*foreach ($oldTypeAttrData as $k => $v){				
				$v['tavalue'] = explode("|",$v['tavalue']);	
				$oldTypeAttrData[$k]['tavalue'] = $v['tavalue'];		
		}*/						   
		//分配到页面		
		View::with('oldData',$oldData);
		//获得货品列表的glid
		
		//获取货品列表数据
		$listData = Db::table('goods_list')->where('goods_gid','=',$gid)->get();
		p($listData);
		//循环数据
		foreach ($listData as $k => $v) {
			//转化成数组
			$listData[$k]['combine'] = explode("|", $v['combine']);
		}
		
		foreach ($listData as $k => $v) {
			$listData[$k]['gtvalue'] = Db::table('goods_attr')
					                     ->where('gtid in ('.implode(",", $v["combine"]).')' )
										 
					                     ->lists('gtvalue');
		}
		
		p($listData);
		//分配页面
		View::with('listData',$listData);
		//载入页面
		View::make();
	}
	/**
	 * 添加
	 */
	public function add()
	{
		//获得gid
		$gid = Q('get.gid',0,'intval');
		$type_tid = Q('get.type_tid',0,'intval');
		//p($type_tid);die;
		if(IS_POST)
		{
			//p($_POST);die;
			if(!$this->db->store())
			{
				//添加失败
				View::error($this->db->getError());
			}else{
				//添加成功
				View::success('添加成功',U('index',array('gid'=>$gid,'type_tid'=>$type_tid)));
			}
		}
	
	}
	/**
	 * 编辑
	 */
	public function edit()
	{
		
		//获得旧数据
		$glid = Q('get.glid',0,'intval');
		$type_tid = Q('get.type_tid',0,'intval');
		//p($type_tid);die;
		//根据$glid获得$goods_gid
		$goods_gid = Db::table('goods_list')->where('glid','=',$glid)->pluck('goods_gid');
		if(IS_POST)
		{
			if(!$this->db->edit($glid))
			{
				//编辑失败
				View::error($this->db->getError($glid));
			}else{
				//编辑成功
				View::success('修改成功',U('index',array('gid'=>$goods_gid,'type_tid'=>$type_tid)));
			}
		}	
		//p($glid);	
		//获得goods_gid		
		$goods_gid = Db::table('goods_list')->where('glid','=',$glid)->pluck('goods_gid');
		//p($goods_gid);
		//获得商品属性表数据
		$type_tid = Db::table('goods')
					       ->where('gid','=',$goods_gid)
						   ->pluck('type_tid');
						   
		//p($type_tid);
		View::with('type_tid',$type_tid);
		//获取类型属性表数据
		$oldData = Db::table('typeattr')->where('type_tid','=',$type_tid)->where('class','=',2)->get();
		//p($oldData);
		View::with('oldData',$oldData);
		//获得商品属性表数据
		//循环压数据
		foreach ($oldData as $k => $v) {
			$oldData[$k]['tavalue'] = Db::table('goods_attr')
							 ->where('typeattr_taid','=',$v['taid'])
			                 ->where('goods_gid','=',$goods_gid)
			                 ->get();
		}
		//p($oldData);
		/*foreach ($oldTypeAttrData as $k => $v){				
				$v['tavalue'] = explode("|",$v['tavalue']);	
				$oldTypeAttrData[$k]['tavalue'] = $v['tavalue'];		
		}*/						   
		//分配到页面		
		View::with('oldData',$oldData);
		
		$glid = Q('get.glid',0,'intval');
		//p($glid);
		$oldListData = Db::table('goods_list')->where('glid','=',$glid)->get();
		//p($oldListData);
		//把字段combine转化成数组
		foreach ($oldListData as $k => $v) {
			//p($v);die;			 
			$oldListData[$k]['gtid'] = explode("|", $v['combine']);
		}
		//p($oldListData);
		//分配到页面
		View::with('oldListData',$oldListData);
		//载入页面
		View::make();
	}
	/**
	 * 删除
	 */
	public function del()
	{
		//获得要删除的glid
		$glid = Q('get.glid',0,'intval');
		//根据$glid获得goods_gid
		$goods_gid = Db::table('goods_list')->where('glid','=',$glid)->pluck('goods_gid');		
		//p($glid);	
		//执行删除
		$this->db->del($glid);
		//删除成功后跳转
		$this->success('删除成功',U('index',array('gid'=>$goods_gid)));
	}
}
